package com.zhaoyizhe.demo.service;

import com.zhaoyizhe.demo.enums.StatusEnum;
import com.zhaoyizhe.demo.mapper.BookInfoMapper;
import com.zhaoyizhe.demo.model.BookInfo;
import com.zhaoyizhe.demo.model.RequestPage;
import com.zhaoyizhe.demo.model.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.addBook(bookInfo);
    }

    public ResultPage getListPage( RequestPage requestPage) {
        // 查找书架一共有多少书
        Integer allBookSize = bookInfoMapper.getAllBookSize();
        // 查找本页需要展示的所有书
        List<BookInfo> pageBooks = bookInfoMapper.getPageBooks(requestPage.getOffset(),requestPage.getPageSize());
        for (BookInfo bookInfo : pageBooks) {
            bookInfo.setStateCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }
        return new ResultPage(pageBooks,allBookSize,requestPage);
    }

    public BookInfo updateGetBookInfo(Integer id) {
        return bookInfoMapper.updateGetBookInfo(id);
    }

    public Integer updateBookInfo(BookInfo bookInfo) {
        return bookInfoMapper.updateBookInfo(bookInfo);
    }

    public Integer batchDeleteBooks(List<Integer> bookIds) {
        return bookInfoMapper.batchDeleteBooks(bookIds);
    }
}
